/**
 * 数据表管理的单例
 */
var SplitTable = {
    id: "splitTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
SplitTable.initColumn = function () {
    var columns = [
        {field: 'selectItem', radio: true},
        {title: '描述', field: 'tableComment', align: 'center', width: '200px', valign: 'middle', sortable: true},
        {title: '表名', field: 'tableName', align: 'center', width: '200px', valign: 'middle', sortable: true},
        {title: '关联字段', field: 'joinField', align: 'center', width: '200px', valign: 'middle', sortable: true},
        {title: '层级', field: 'levels', align: 'center', width: '50px', valign: 'middle', sortable: true},
        {title: '创建日期', field: 'createDate', align: 'center', width: '200px', valign: 'middle', sortable: true},
        {title: '更新日期', field: 'updateDate', align: 'center', width: '200px', valign: 'middle', sortable: true},
        {title: '操作', field: 'id', align: 'center', width: '100px', valign: 'middle',formatter:function(value,row,index){
    		var _html = '<a href="javascript:SplitTable.edit(\''+row.id+'\')" title="修改"><i class="hisui-icon icon-write-order"></i></a>';
    		_html += '<a href="javascript:SplitTable.del(\''+row.id+'\',\''+row.tableName+'\')" title="删除"><i class="hisui-icon icon-cancel"></i></a>';
    		return _html;
        }}];
    return columns;
};

/**
 * 检查是否选中
 */
SplitTable.check = function () {
    var selected = $('#' + this.id).bootstrapTreeTable('getSelections');
    if (selected.length == 0) {
        Sys.info("请先选中表格中的某一记录！");
        return false;
    } else {
        SplitTable.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加操作表
 */
SplitTable.add = function () {
	this.layerIndex = Sys.open('新增操作表',Sys.backendPath + '/splittable/to_add',['550px', '450px']);
};

/**
 * 点击修改按钮时
 */
SplitTable.edit = function (id) {
	var flag = false;
	if(id){
		flag = true;
	}else if (this.check()) {
        id = this.seItem.id;
        flag = true;
    }
	if (flag) {
		this.layerIndex = Sys.open('修改操作表',Sys.backendPath + '/splittable/to_edit/' + id,['550px', '450px']);
    }
};

/**
 * 删除操作表
 */
SplitTable.del = function (id,name) {
	var flag = false;
	if(id){
		flag = true;
	}else if (this.check()) {
        id = this.seItem.id;
        name = this.seItem.name
        flag = true;
    }
	if (flag) {
        var operation = function(){
	        var ajax = new $ax(Sys.backendPath + "/splittable/remove", function (data) {
                if (data.code == 200) {
                    Sys.success("删除成功!");
                    SplitTable.table.refresh();
                } else {
                    Sys.error("删除失败!" + data.message + "!");
                }
	        }, function (data) {
	            Sys.error("删除失败!" + data.responseJSON.message + "!");
	        });
	        ajax.set("splitTablesId", id);
	        ajax.start();
	    };
	    Sys.confirm("是否删除操作表【" + name + "】?",operation);
    }
};

/**
 * 搜索操作表
 */
SplitTable.search = function () {
    var queryData = {};
    queryData['tableCode'] = $("#tableCode").val();
    SplitTable.table.refresh({query: queryData});
}

$(function () {
    var defaultColunms = SplitTable.initColumn();
    var table = new BSTreeTable(SplitTable.id, Sys.backendPath + "/splittable/list", defaultColunms);
    table.setExpandColumn(1);
    table.setIdField("id");
    table.setParentIdField("parentId");
    table.setExpandAll(true);
    table.setShowColumns(false);
    table.setShowRefresh(false);
    table.init();
    SplitTable.table = table;
});
